import { clipboard } from '@ve/utils/clipboard'
import { jsonToTable } from '@ve/utils/jsonToTable'
import { ElMessage } from 'element-plus'
import { translateTitle } from '@/utils/i18n'
//必须带done回调 执行完成调用
export default function copyTableRow({ action, additionalData, done }) {
  if (!additionalData) {
    ElMessage({
      showClose: true,
      message: translateTitle('Ve.eventTask.复制单行提示'),
      type: 'error',
    })
    done(null, {})
    return
  }
  let _hasHeader = action.extension.hasHeader.value || false
  let _tableColumns = additionalData.tableFun.getTableColumns

  let _header = {}
  _tableColumns.forEach((v) => {
    if (v.type !== 'checkbox' && v.type !== 'seq') {
      _header[v.field] = v.title
    }
    if (v.type == 'seq') {
      _header['#SEQ#'] = v.title
    }
  })

  let _tableRows = []
  let rows = [additionalData.row]

  rows.forEach((v) => {
    let _row = {}
    Object.keys(_header).forEach((vv) => {
      if (vv != 'undefined') {
        _row[vv] = v[vv]
        if (vv == '#SEQ#') {
          _row[vv] = additionalData.rowIndex + 1
        }
      }
    })
    _tableRows.push(_row)
  })

  let tableTtml = jsonToTable({
    showHeader: _hasHeader,
    data: _tableRows,
    header_mapping: _header,
  })
  if (tableTtml) {
    clipboard(tableTtml)
  }

  done(null, {})
}
